package com.aaa.controller.system;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.Result;
import com.aaa.entity.system.Menu;
import com.aaa.entity.system.PageOrder;
import com.aaa.entity.system.Role;
import com.aaa.service.system.MenuService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2022-08-30 08:26:04
 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private MenuService menuService;


    @RequestMapping("queryAllMenu.do")
    public Object queryAllMenu() {
        List<Tree<String>> trees = menuService.queryAllMenu();
        if (ObjectUtil.isNotEmpty(trees)) {
            JSONConfig jsonConfig = JSONConfig.create();
            jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
            String jsonStr = JSONUtil.toJsonStr(success(trees), jsonConfig);
            return jsonStr;
        }
        return fail();
    }

    @RequestMapping("queryAllMenus.do")
    public Object queryAllMenus() {
        List<Tree<String>> trees = menuService.queryAllMenus();
        if (ObjectUtil.isNotEmpty(trees)) {
            return success(trees);
        }
        return fail();
    }

    @RequestMapping("findParentNameId.do")
    public Object findParentNameId(int menuId) {
       List<Menu> list =  menuService.findParentNameId(menuId);
       if(ObjectUtil.isNotEmpty(list)){
           return success(list);
       }
       return fail();
    }

    @RequestMapping("deleteMenuById.do")
    public Object deleteMenuById(int menuId) {
       boolean b =  menuService.deleteMenuById(menuId);
       if(b){
           return success();
       }
       return fail();
    }

    @RequestMapping("insertMenu.do")
    public Object insertMenu(Menu menu) {
       boolean b =  menuService.insertMenu(menu);
       if(b){
           return success();
       }
       return fail();
    }
    @RequestMapping("updateMenu.do")
    public Object updateMenu(Menu menu) {
       boolean b =  menuService.updateMenu(menu);
       if(b){
           return success();
       }
       return fail();
    }



    //用户查询页面
    @RequestMapping("toShowMenu.do")
    public ModelAndView toShowUser(ModelAndView modelAndView){
        modelAndView.setViewName("system/showMenu");
        return modelAndView;
    }

}

